<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\ClientUser;
use App\Models\Client;
use App\Models\Wxuser;

class ClientUserController extends Controller
{
    public function index(Request $request)
    {
        $where = [];
        $query = ClientUser::query();
        if($request->client_id) {
        	$query->where('client_id',$request->client_id);
        	$where['client_id'] = $request->client_id;
        }
        if($request->keyword) {
            $query->where('name','like','%'.$request->keyword.'%')->orWhere('phone','like','%'.$request->keyword.'%');
            $where['keyword'] = trim($request->keyword);

        }
        $list = $query->paginate(20);
        $clients  = Client::all();
        $wxusers  = Wxuser::get();
        return view('admin.client-user.index',compact('list','where','clients','wxusers'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
       
        $wxusers  = Wxuser::get();
        return view('admin.client-user.add',compact('wxusers'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $rs['status'] = 'danger';
        $rs['msg']    = '操作失败';
        $data = $request->except(['_token']);
        //dd($data);      
       
        $flag = ClientUser::create($data);
        if($flag) {
            $rs['status'] = 'success';
            $rs['msg'] = '操作成功';
            return back()->with('rs',$rs);
        }
        
        return back()->with('rs',$rs);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $data     = ClientUser::find($id);
      	$clients  = Client::all();
        $wxusers  = Wxuser::get();
        return view('admin.client-user.edit',compact('data','wxusers','clients'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $data = $request->except('_token','_method');
        //$data['updater'] = session('admin')->username;
        $rs   = ClientUser::where('id',$id)->update($data);
        if($rs) {
            return redirect('zadmin/client-users');
        }
        return back()->withInput();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $rs['status'] = 'danger';
        $rs['msg'] = '操作失败';
        $flag = ClientUser::destroy($id);
        if($flag) {
            $rs['status'] = 'success';
            $rs['msg']    = '操作成功';
            return back()->with('rs',$rs);
        }       
       
        return back()->with('rs',$rs);
    }
}
